package net.zyj.student.mapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import net.zyj.student.pojo.Student;
import net.zyj.student.util.DigestUtil;

public class StudentMapper {
	
	/**
	 * 学生登录验证
	 * @param card
	 * @param password
	 * @return
	 */
	public List<Student> login(String card,String password) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		Map<String, String> map = new HashMap<String, String>();
		map.put("card", card);
		map.put("password", password);
		try {
			return session.selectList("net.zyj.student.mapper.StudentMapper.login",map);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 辅导员可管理的学生列表
	 * @param map
	 * @return
	 */
	public List<Student> selectListByteacherId(Map<String, Object> map) {
		SqlSession session=DBSessionFactory.getSqlSessionFactory().openSession();
		try {
			return session.selectList("net.zyj.student.mapper.StudentMapper.selectListByteacherId",map);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	/**
	 * 辅导员可管理的学生总数
	 * @param map
	 * @return
	 */
	public int countByteacherId(Map<String, Object> map) {
		SqlSession session=DBSessionFactory.getSqlSessionFactory().openSession();
		try {
			return session.selectOne("net.zyj.student.pojo.countByteacherId",map);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 新增一条学生信息
	 * @param student
	 * @return
	 */
	public int save(Student student) {
		String password="1111";
		student.setPassword(DigestUtil.encodeSHA512Hex(password.getBytes()));
		student.setEnterDate(new java.sql.Date(new java.util.Date().getTime()));
		SqlSession session=DBSessionFactory.getSqlSessionFactory().openSession();
		int count=session.insert("net.zyj.student.mapper.StudentMapper.save",student);
		session.commit();
		return count;
	}
	/**
	 * 根据id查找学生信息
	 * @param id
	 * @return
	 */
	public Student selectById(Integer id) {
		SqlSession session=DBSessionFactory.getSqlSessionFactory().openSession();
		return session.selectOne("net.zyj.student.mapper.StudentMapper.selectById",id);
	}
	/**
	 * 更新学生信息
	 * @param student
	 * @return
	 */
	public int update(Student student) {
		SqlSession session=DBSessionFactory.getSqlSessionFactory().openSession();
		int count=session.insert("net.zyj.student.mapper.StudentMapper.update",student);
		session.commit();
		return count;
	}
	/**
	 * 根据id删除学生信息
	 * @param id
	 * @return
	 */
	public int deleteById(Integer id) {
		SqlSession session=DBSessionFactory.getSqlSessionFactory().openSession();
		int count=session.delete("net.zyj.student.mapper.StudentMapper.deleteByid",id);
		session.commit();
		return count;
	}
}
